<?php
/**
 * DeductController.class.ph
 * 分成控制器
 * 
 * @author  王中艺 <wangzy_smile@qq.com>
 * @date    2017-08-29
 */

namespace Attach\Controller;

class DeductController extends BaseController {

    /**
     * 分成记录获取
     * @param   $condition
     * @param   $cate  1.课程；2.购物车；3.vip
     * @param   $p
     * @return  $list and $page_html
     */
    function getList(){
        $condition  = $this->request('condition', []);
        $condition['user_id']   = $this->userId;
        $cate   = $this->request('cate', 1);
        $page   = $this->request('p', 1);
        $pageHtml   = true;

        switch($cate){
            case 1:
                $list   = D('Deduct')->getCourseList($condition, $page, $pageHtml);
            break;
            case 2:
                $list   = D('Deduct')->getShopCartList($condition, $page, $pageHtml);
            break;
            case 3:
                $list   = D('Deduct')->getVipList($condition, $page, $pageHtml);
            break;
            default: break;
        }

        $this->response([
            'list'  => $list,
            'page_html' => $pageHtml
        ]);
    }
    
    /**
     * 导出excel
     * @param   $condition
     * @param   $cate
     * @param   $start
     * @param   $length
     * @return  $file
     */
    function outExcel(){
        $condition  = $this->request('condition', []);
        $condition['user_id']   = $this->userId;
        $cate   = $this->request('cate', 1);
        $start  = $this->request('start', false);
        $length     = $this->request('length', false);

        if($start === false || $length === false){
            //返回可导出数量
            $page   = false;
            $pageHtml   = false;
            $count  = true;
            switch($cate){
                case 1:
                    D('Deduct')->getCourseList($condition, $page, $pageHtml, $count);
                break;
                case 2:
                    D('Deduct')->getShopCartList($condition, $page, $pageHtml, $count);
                break;
                case 3:
                    D('Deduct')->getVipList($condition, $page, $pageHtml, $count);
                break;
                default:break;
            }

            $this->response(['count' => $count]);
        }else{
            //导出数据
            $page   = [
                'start' => $start,
                'length'    => $length
            ];
            switch($cate){
                case 1:
                    $list   = D('Deduct')->getCourseList($condition, $page);
                break;
                case 2:
                    $list   = D('Deduct')->getShopCartList($condition, $page);
                break;
                case 3:
                    $list   = D('Deduct')->getVipList($condition, $page);
                break;
                default: break;
            }

            $title  = [
                '分成ID', '微信昵称', '真实姓名', '交易类型',
                '交易金额', '交易时间', '分成金额', '分成比例'
            ];
            foreach($list as &$item){
                $i  = [
                    $item['id'],
                    $item['nickname'],
                    $item['truename'],
                    $item['goods_type_text'],
                    $item['total_amount'],
                    $item['create_date'],
                    $item['amount'],
                    $item['percent'],
                ];
                $item   = $i;
            }

            out_excel($title, $list, 'deduct_list_'.$cate.'_'.$start);
        }
    }
}